<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='发布菜谱',active='publish'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<link th:href="@{/back/plugins/tagsinput/jquery.tagsinput.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/select2.dist.css/select2-bootstrap.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/toggles/toggles.css}" rel="stylesheet"/>

<link th:href="@{/back/plugins/mditor/css/mditor.min.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.css}" rel="stylesheet">
<style>
    #tags_tagsinput {
        background-color: #fafafa;
        border: 1px solid #eeeeee;
    }

    #tags_addTag input {
        width: 100%;
    }

    #tags_addTag {
        margin-top: -5px;
    }

    .mditor .editor{
        font-size: 14px;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    }
    .mditor .backdrop, .mditor .textarea, .mditor .viewer{
        font-size: 14px;
    }
    .markdown-body{
        font-size: 14px;
    }
    .note-toolbar {
        text-align: center;
    }

    .note-editor.note-frame {
        border: none;
    }

    .note-editor .note-toolbar {
        background-color: #f5f5f5;
        padding-bottom: 10px;
    }

    .note-toolbar .note-btn-group {
        margin: 0;
    }

    .note-toolbar .note-btn {
        border: none;
    }

    #articleForm #dropzone {
        min-height: 200px;
        background-color: #dbdde0;
        line-height:200px;
        margin-bottom: 10px;
    }
    #articleForm .dropzone {
        border: 1px dashed #8662c6;
        border-radius: 5px;
        background: white;
    }
    #articleForm .dropzone .dz-message {
        font-weight: 400;
    }
    #articleForm .dropzone .dz-message .note {
        font-size: 0.8em;
        font-weight: 200;
        display: block;
        margin-top: 1.4rem;
    }
</style>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">
                            <th:block th:if="${null != menu}">
                                编辑菜谱
                            </th:block>
                            <th:block th:unless="${null != menu}">
                                发布菜谱
                            </th:block>
                        </h4>
                    </div>
                    <div class="col-md-12">
                        <form id="menuForm">
                            <input type="hidden" name="id"
                                   th:value="${menu!=null and menu.id!=null}?${menu.id}: ''" id="id"/>
                            <input type="hidden" name="content" id="content-editor"/>
                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <th:block th:if="${null != menu}">
                                    <input type="text" class="form-control" name="title"
                                           required="required" aria-required="true" th:value="${menu.title}"/>
                                </th:block>
                                <th:block th:unless="${null != menu}">
                                    <input type="text" class="form-control" placeholder="请输入菜谱标题（必须）" name="title"
                                           required="required" aria-required="true"/>
                                </th:block>
                            </div>
                            <div class="form-group col-md-3" style="padding: 0 10px 0 0;">
                                <th:block th:if="${null != menu}">
                                    <input name="image" id="image" type="text" class="form-control"  th:value="${menu.image}" />
                                </th:block>
                                <th:block th:unless="${null != menu}">
                                    <input name="image" id="image" type="text" class="form-control"  placeholder="请输入菜谱主图链接" />
                                </th:block>
                            </div>
                            <div class="form-group col-md-3" style="padding: 0 10px 0 0;">
                                <th:block th:if="${null != menu}">
                                    <input name="tag" id="tag" type="text" class="form-control" th:value="${menu.tag}" />
                                </th:block>
                                <th:block th:unless="${null != menu}">
                                    <input name="tag" id="tag" type="text" class="form-control"  placeholder="请输入菜谱标签" />
                                </th:block>
                            </div>
                            <div class="clearfix"></div>
                            <div id="md-container" class="form-group">
                                <textarea id="md-editor"  th:utext="${menu!=null and menu.content !=null}?${menu.content}: ''"></textarea>
                            </div>

                            <div class="clearfix"></div>

                            <div class="text-right">
                                <a class="btn btn-default waves-effect waves-light" th:href="@{/business/menu}">返回订单列表</a>
                                <button type="button" class="btn btn-primary waves-effect waves-light" onclick="submenu('publish');">
                                    保存菜谱
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>

<script th:src="@{/back/plugins/tagsinput/jquery.tagsinput.min.js}"></script>
<script th:src="@{/back/plugins/jquery-multi-select/jquery.quicksearch.js}"></script>

<script th:src="@{/back/plugins/mditor/js/mditor.min.js}"></script>
<script th:src="@{/back/plugins/multi-select/0.9.12/js/jquery.multi-select.min.js}"></script>
<script th:src="@{/back/plugins/select2/3.4.8/select2.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script>
<script th:src="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.js}"></script>
<script th:src="@{/back/plugins/to-markdown/3.1.0/to-markdown.min.js}"></script>

<script type="text/javascript">
    var mditor;
    var tale = new $.tale();
    var attach_url = $('#attach_url').val();
    // 每10分钟自动保存一次草稿
    // var refreshIntervalId = setInterval("autoSave()", 10 * 60 * 1000);
    Dropzone.autoDiscover = false;
    $(document).ready(function () {

        mditor = window.mditor = Mditor.fromTextarea(document.getElementById('md-editor'));

        // Tags Input
        $('#tags').tagsInput({
            width: '100%',
            height: '35px',
            defaultText: '请输入菜谱标签'
        });

        $('.toggle').toggles({
            on: true,
            text: {
                on: '开启',
                off: '关闭'
            }
        });

        $("#multiple-sel").select2({
            width: '100%'
        });

        $('div.allow-false').toggles({
            off: true,
            text: {
                on: '开启',
                off: '关闭'
            }
        });

        if($('#thumb-toggle').attr('thumb_url') != ''){
            $('#thumb-toggle').toggles({
                off: true,
                text: {
                    on: '开启',
                    off: '关闭'
                }
            });
            $('#thumb-toggle').attr('on', 'true');
            $('#dropzone').css('background-image', 'url('+ $('#thumb-container').attr('thumb_url') +')');
            $('#dropzone').css('background-size', 'cover');
            $('#dropzone-container').show();
        } else {
            $('#thumb-toggle').toggles({
                off: true,
                text: {
                    on: '开启',
                    off: '关闭'
                }
            });
            $('#thumb-toggle').attr('on', 'false');
            $('#dropzone-container').hide();
        }

        var thumbdropzone = $('.dropzone');
    });


    function submenu(status) {
        var title = $('#menuForm input[name=title]').val();
        var content =  mditor.value;
        if (title == '') {
            tale.alertWarn('请输入菜谱标题');
            return;
        }
        if (title .length>25) {
            tale.alertWarn('菜谱标题不能超过25个字符！');
            return;
        }
        if (content == '') {
            tale.alertWarn('请输入菜谱内容');
            return;
        }
        $('#content-editor').val(content);
        $("#menuForm #status").val(status);
        $("#menuForm #categories").val($('#multiple-sel').val());
        var params = $("#menuForm").serialize();
        var url = $('#menuForm #id').val() != '' ? '/business/menu/modify' : '/business/menu/publish';
        tale.post({
            url:url,
            data:params,
            success: function (result) {
                if (result && result.success) {
                    tale.alertOk({
                        text:'菜谱保存成功',
                        then: function () {
                            setTimeout(function () {
                            }, 500);
                        }
                    });
                } else {
                    tale.alertError(result.msg || '保存菜谱失败！');
                }
            }
        });
    }

    function allow_comment(obj) {
        var this_ = $(obj);
        var on = this_.find('.toggle-on.active').length;
        var off = this_.find('.toggle-off.active').length;
        if (on == 1) {
            $('#allow_comment').val(false);
        }
        if (off == 1) {
            $('#allow_comment').val(true);
        }
    }

</script>
</body>
</html>